define(function (require, exports, module) {
    exports.ready = function () {
        require.async('$', function () {
            $(function () {
                var store_id = $('#store_id').val(),
                    order_id = $('#order_id').val(),option_flag = true;

                getData();

                $(document).on('click','.icon-xing',function(){
                    $(this).addClass('checked');
                    $(this).prevAll('.icon-xing').addClass('checked');
                    $(this).nextAll('.icon-xing').removeClass('checked');
                });

                $('#comment').click(function(){
                    option_flag = false;
                    var server_star = 0,order_comment = [];
                    if($('#server .checked').length == 0){
                        fm.alert('请对服务态度进行评分','background:#fff;color:#2a2a2a;');
                    }else{
                        $('.goods_block').each(function(){
                            server_star = Number($('#server .checked').length);
                            var other_star = Number($(this).find('.checked').length);
                            var goods_id = $(this).data('goods_id');
                            var goods_star = ((other_star + server_star) / 3).toFixed(1);
                            var comment_picture = [];
                            if($(this).find('.album_list').length > 0){
                                $(this).find('.album_list').each(function(){
                                    comment_picture.push($(this).find('img').attr('src'));
                                })
                            }
                            order_comment.push({order_id:order_id,goods_id:goods_id,goods_star:goods_star,comment_picture:comment_picture})
                        })

                        $.ajax({
                            url: '/restaurant/TrustworthyShop/addComment',
                            type: 'post',
                            data: {store_id: store_id,order_comment:order_comment},
                            dataType: 'json',
                            beforeSend: function () {
                                option_flag = false;
                                $('.circle-loadding').show();
                            },
                            success: function (data) {
                                if(data.status == 'S'){
                                    fm.alert(data.message,'background:#fff;color:#2a2a2a');
                                    setTimeout(function(){
                                        location.href = '/restaurant/TrustworthyShop/usercenter/store_id/'+store_id;
                                    },500);
                                }else{
                                    fm.alert(data.message,'background:#fff;color:#2a2a2a');
                                }
                                option_flag = true;
                                $('.circle-loadding').hide();
                            }
                        })

                    }
                })

                function getData(){
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/commentShow',
                        type: 'post',
                        data: {store_id: store_id,order_id:order_id},
                        dataType: 'json',
                        beforeSend: function () {
                            $('.circle-loadding').show();
                        },
                        success: function (data) {
                            if(data.status == 'S'){
                                var html = '';
                                if(data.cartCount > 0){
                                    $('.tips').html(data.cartCount).removeClass('hide');
                                }

                                if(data.commentData.length > 0 ){
                                    $.each(data.commentData,function(i,k){
                                        html += '<div class="gc-block full item list-item radius-none goods_block" data-goods_id="'+ k.goods_id +'"><div class="gc-flex gc-flex-lc gc-block-line">' +
                                            '<div class="item-img"><img src="'+ k.goods_thumb +'"></div><div class="item-msg gc-flex-item">' +
                                            '<p class="gc-ft-gray ellipsis">'+ k.goods_name +'</p><div class="score score-option">' +
                                            '<p><span class="score-name">商品优质</span><span class="iconfont icon-xing checked"></span><span class="iconfont icon-xing checked"></span>' +
                                            '<span class="iconfont icon-xing checked"></span><span class="iconfont icon-xing checked"></span><span class="iconfont icon-xing checked"></span></p>' +
                                            '<p><span class="score-name">商品原料</span><span class="iconfont icon-xing checked"></span><span class="iconfont icon-xing checked"></span>' +
                                            '<span class="iconfont icon-xing checked"></span><span class="iconfont icon-xing checked"></span><span class="iconfont icon-xing checked"></span></p>' +
                                            '</div></div></div><div class="album gc-flex gc-flex-wrap gc-flex-lc gc-block-line album-'+ k.goods_id +'">' +
                                            '<div class="img-box upload_btn gc-ft-gray gc-flex gc-flex-col upload_btn-'+ k.goods_id +'">' +
                                            '<p class="iconfont icon-jia1 gc-ft-center"></p><input class="input_upload" data-goods_id="'+ k.goods_id +'" type="file" accept="image/*"/></div></div></div>';
                                    })
                                }
                                $('#comment_list').html(html);
                            }
                            $('.circle-loadding').hide();
                        }
                    })
                }

                $(document).on('click','.delete',function(){
                    var goods_id = $(this).data('goods_id');
                    $(this).parents('.img-box').remove();
                    imglength = $('.album-'+goods_id).find('.album_list').length;
                    if(imglength >= 3){
                        $('.album-'+goods_id).append('<div class="img-box upload_btn gc-ft-gray gc-flex gc-flex-col upload_btn-'+ goods_id +'">' +
                            '<p class="iconfont icon-jia1 gc-ft-center"></p><input class="input_upload" data-goods_id="'+ goods_id +'" type="file" accept="image/*"/></div>');
                    }
                })

                var imglength = 0,upload_img = true;
                require.async(['upload','fileupload'], function(){
                    //图片上传
                    $(document).on('click','.input_upload',function(){
                        var goods_id = $(this).data('goods_id');
                        $(".upload_btn").fileupload({
                            url:"/restaurant/TrustworthyShop/upload/store_id/" + store_id,
                            beforeSend: function(){
                                $('.circle-loadding').show();
                            },
                            done:function(e,result){
                                if(upload_img){
                                    var data=$.parseJSON(result.result);
                                    // console.log(data);
                                    if(data.status=='S'){
                                        var html = '<div class="img-box album_list"><img data-url="'+ data.url +'" src="http://'+data.url+'"/><span class="iconfont icon-delete delete" data-goods_id="'+ goods_id +'"></span></div>';
                                        $('.album-'+goods_id).prepend(html);
                                        imglength = $('.album-'+goods_id).find('.album_list').length;
                                        if(imglength >= 4){
                                            $('.upload_btn-'+goods_id).remove();
                                        }
                                        upload_img = true;
                                        $('.circle-loadding').hide();
                                    }else{
                                        fm.alert(data.message,'background: #fff;color: #2a2a2a;');
                                        upload_img = false;
                                    }
                                }
                            }
                        });
                    })

                })

            });
        });
    };
});
